/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.tree;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.file.JXFile;
import org.jxUtils.xml.XMLUtil;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.tree
 * @date 05/02/2014 - 22:51:32
 */
public class Tree extends JTree implements JXComponent {
	
	private static final long serialVersionUID = -4247139309982629271L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	/**
	 *
	 */
	public Tree() {
	
		super();
	}
	
	/**
	 * @param xmlFile
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws IOException
	 */
	public Tree(final File xmlFile) throws IOException, ParserConfigurationException, SAXException {
	
		this();
		
		fromXML(xmlFile);
	}
	
	/**
	 * @param xmlFile
	 * @param showDetails
	 * @param showType
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public Tree(final File xmlFile, final boolean showDetails, final boolean showType) throws IOException, ParserConfigurationException, SAXException {
		
		this();
		
		fromXML(xmlFile, showDetails, showType);
	}
	
	/**
	 * @param value
	 */
	public Tree(final Hashtable<?, ?> value) {
	
		super(value);
	}
	
	/**
	 * @param value
	 */
	public Tree(final Object[] value) {
	
		super(value);
	}
	
	/**
	 * @param newModel
	 */
	public Tree(final TreeModel newModel) {
	
		super(newModel);
	}
	
	/**
	 * @param root
	 */
	public Tree(final TreeNode root) {
	
		super(root);
	}
	
	/**
	 * @param root
	 * @param asksAllowsChildren
	 */
	public Tree(final TreeNode root, final boolean asksAllowsChildren) {
	
		super(root, asksAllowsChildren);
	}
	
	/**
	 * @param value
	 */
	public Tree(final Vector<?> value) {
	
		super(value);
	}
	
	/**
	 * @param xmlFile
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public final void fromXML(final File xmlFile) throws IOException, ParserConfigurationException, SAXException {
	
		fromXML(xmlFile, true, false);
	}
	
	/**
	 * @param xmlFile
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 */
	public final void fromXML(final File xmlFile, final boolean showDetails, final boolean showType) throws IOException, ParserConfigurationException, SAXException {
	
		setModel(XMLUtil.read(xmlFile, showDetails, showType));
	}
	
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/**
	 * <b>Reference:</b> http://cse.unl.edu/~reich/XML/DOM/XML2JTree.java <br>
	 * <br>
	 * This simple method returns a displayable string given a NodeType
	 * 
	 * @param node
	 * @return type
	 */
	public final String getNodeType(final Node node) {
	
		String type;
		
		//
		switch(node.getNodeType()) {
		
			case Node.ELEMENT_NODE: {
				
				type = "Element";
				break;
				
			}
			case Node.ATTRIBUTE_NODE: {
				
				type = "Attribute";
				break;
				
			}
			case Node.TEXT_NODE: {
				
				type = "Text";
				break;
				
			}
			case Node.CDATA_SECTION_NODE: {
				
				type = "CData section";
				break;
				
			}
			case Node.ENTITY_REFERENCE_NODE: {
				
				type = "Entity reference";
				break;
				
			}
			case Node.ENTITY_NODE: {
				
				type = "Entity";
				break;
				
			}
			case Node.PROCESSING_INSTRUCTION_NODE: {
				
				type = "Processing instruction";
				break;
				
			}
			case Node.COMMENT_NODE: {
				
				type = "Comment";
				break;
				
			}
			case Node.DOCUMENT_NODE: {
				
				type = "Document";
				break;
				
			}
			case Node.DOCUMENT_TYPE_NODE: {
				
				type = "Document type";
				break;
				
			}
			case Node.DOCUMENT_FRAGMENT_NODE: {
				
				type = "Document fragment";
				break;
				
			}
			case Node.NOTATION_NODE: {
				
				type = "Notation";
				break;
				
			}
			default: {
				
				type = "Unknown, contact Sun!";
				break;
			}
		}
		
		return type;
	}
	
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	/**
	 * @param iconsMap
	 *        Hashtable< label, icon >
	 */
	public final void setIconsMap(final Hashtable<String, Icon> iconsMap) {
	
		putClientProperty("JTree.icons", iconsMap);
	}
	
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Tree.editorBorder"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
	
	/**
	 * Sets the selection model, which must be one of SINGLE_TREE_SELECTION,
	 * CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.
	 * <p>
	 * This may change the selection if the current selection is not valid for the new mode. For example, if three TreePaths are selected when the mode is changed to <code>SINGLE_TREE_SELECTION</code>, only one TreePath will remain selected. It is up to the particular implementation to decide what TreePath remains selected.
	 */
	public final void setSelectionMode(final int mode) {
	
		getSelectionModel().setSelectionMode(mode);
	}
	
	/**
	 * @return xml String
	 * @throws ParserConfigurationException
	 * @throws TransformerException
	 */
	public final String toXML() throws ParserConfigurationException, TransformerException {
	
		return XMLUtil.toXML(getModel());
	}
	
	/**
	 * @param file
	 * @throws IOException
	 * @throws ParserConfigurationException
	 * @throws TransformerException
	 */
	public final void toXML(final File file) throws IOException, ParserConfigurationException, TransformerException {
	
		new JXFile(file).writeString(toXML());
	}
}
